package com.example.pingze.controller;

import com.example.pingze.dto.PoemAndPoetInfoDto;
import com.example.pingze.entity.PoemInfo;
import com.example.pingze.entity.Sentence;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.service.PoemInfoService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/poemInfo")
@CrossOrigin(origins = "*")
public class PoemInfoController {
    @Autowired
    private PoemInfoService service;
    //诗人名称获取古诗信息
    @PostMapping("/findPoemInfoByWriter")
    public Result<List<PoemInfo>> findPoemInfoByWriter(@RequestBody PoemInfo poemInfo){
        if(poemInfo.getWriter() == null){
            throw new PingZeException("作者名称获取失败");
        }
        String writer = poemInfo.getWriter();
        List<PoemInfo> info = service.findPoemInfoByWriter(writer);
        return ResultVOUtils.result(info);
    }
    //通过id获取诗人和古诗的详细信息
    @PostMapping("/findAllById")
    public Result<PoemAndPoetInfoDto> findAllById(@RequestBody PoemInfo poemInfo){
        if(poemInfo.getId() == null){
            throw new PingZeException("诗词ID为空");
        }
        PoemAndPoetInfoDto info = service.findAllById(poemInfo.getId());
        return ResultVOUtils.result(info);
    }
    //多条件（诗人，朝代，类型）查询古诗信息
    @RequestMapping("/findPoemInfo")
    public Result<List<PoemInfo>> findPoemInfo(@RequestBody PoemInfo poemInfo){
        List<PoemInfo> poemInfoList = service.findPoemInfoList(poemInfo);
        if(poemInfoList == null){
            throw new PingZeException("诗人列表为空！");
        }
        return ResultVOUtils.result(poemInfoList);
    }

    @PostMapping("/getWriter")
    public Result<List<String>> getWriter(){
        List<String> list = service.getWriter();
        return ResultVOUtils.result(list);
    }
    //获取通过诗人和诗名获取名句
    @PostMapping("/getPoemIdByWriterAndTitle")
    public Result<Sentence> getSentences(@RequestParam String writer,
                                         @RequestParam String title){
        String id = null;
        if(writer == null){
            throw new PingZeException("作者信息为空");
        }
        if(title == null){
            throw new PingZeException("诗名为空");
        }
        PoemInfo poemInfo= service.getPoemInfoByWriterAndTitleContains(writer,title);
        if (poemInfo != null) {
            id = poemInfo.getId();
        }
        return ResultVOUtils.success(id);
    }
    /**
     * 后台管理系统
     */
    @CrossOrigin
    @PostMapping("/page")
    public Result<Page<PoemInfo>> getList(
            PoemInfo poem,
            @RequestParam(name="pageCode",required = false,defaultValue = "1") Integer pageCode,
            @RequestParam(name="pageSize",required = false, defaultValue = "2") Integer pageSize
    ){
        if(pageCode<1){
            throw new PingZeException("页码不能小于1");
        }
        Sort.Order idOrder = Sort.Order.asc("id");
        Sort sort = Sort.by(idOrder);
        Pageable pageable = PageRequest.of(pageCode-1,pageSize,sort);
        Page page = service.getPoemInfoList(poem, pageable);
        return ResultVOUtils.result(page);
    }
    @CrossOrigin
    @PostMapping("/add")
    public Result<PoemInfo> add(String title, String dynasty, String writer, String type,
                                String content, String remark, String translation, String appreciation){
        PoemInfo add = service.add(
                title, dynasty, writer, type,
                content, remark, translation, appreciation);
        return ResultVOUtils.result(add);
    }
    @CrossOrigin
    @PostMapping("/edit")
    public Result<PoemInfo> edit(String id, String title, String dynasty, String writer,
                                 String type, String content, String remark,
                                 String translation, String appreciation){
        PoemInfo edit = service.edit(
                id,title, dynasty, writer, type, content,
                remark, translation, appreciation);
        return ResultVOUtils.result(edit);
    }
    @CrossOrigin
    @Transactional
    @PostMapping("/delete")
    public Result<String> delete(String id){
        if(id == null){
            throw new PingZeException("id为空");
        }
        PoemInfo poemInfo = service.findPoemInfoById(id);
        if(poemInfo != null){
            service.delete(poemInfo);
        }
        return ResultVOUtils.success("delete success");
    }
}
